#include"PacketResolver.h"

PacketResolver::PacketResolver(SafeQueue<Packet_t>& queue):packetQueue(queue) {
    worker->runInLoop([&] {
        printf("----- PacketResolver Running in tid % ld -----\n", hv_gettid());
        printf("%-15s || %-15s || %-4s\n", "src", "dst", "len");
        while (true) {
            Packet_t packet;
            if (this->packetQueue.Consume(packet)) {
                //for debug
                unsigned char* ip_hdr = packet.data;
                char src[20] = { 0 };
                inet_ntop(AF_INET, ip_hdr + 12, src, sizeof(src));
                char dst[20] = { 0 };
                inet_ntop(AF_INET, ip_hdr + 16, dst, sizeof(dst));
                printf("%-15s || %-15s || %-4d\n", src, dst, packet.len);
                //printf("packet received");

                hv_free(packet.data);//debug but dont forget to free！
			}
		}
	});
}

void PacketResolver::startResolving() {
    this->workerThread.start();
}

void PacketResolver::stopResolving() {
    if (workerThread.isRunning()) {
        this->workerThread.stop();
    }
}

PacketResolver::~PacketResolver() {
    stopResolving();
}